<template>
  <div class="node-info">
    <el-scrollbar class="node-info-body">
      <el-tabs v-model="type"
               @tab-click="handleClick">
        <el-tab-pane :label="currentNode.nodeName"
                     disabled></el-tab-pane>
        <el-tab-pane :label="$t('processDialog.basicSetting')"
                     name="1">
          <el-form class="node-form">
            <!-- 节点名称 -->
            <el-form-item :label="$t('processDialog.nodeNameText')">
              <el-input :disabled="!currentNode.isEditable && nodeMaintain"
                        v-model="currentNode.nodeName"></el-input>
            </el-form-item>
            <!-- 代理类型 -->
            <el-form-item :label="$t('processDialog.agentType')"
                          v-show="currentNode.nodeType === 3 || currentNode.nodeType === 7">
              <el-select v-model="currentNode.assigneeType"
                         :disabled="!currentNode.isEditable && nodeMaintain"
                         @change="changeAssignee">
                <el-option v-for="item in assigneeOption"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <!-- 代理人/部门等 -->
            <el-form-item :label="$t('processDialog.agent')+assigneeName"
                          v-if="currentNode.nodeType === 3 || currentNode.nodeType === 7"
                          :rules="[{required:true,message:$t('processDialog.pleaseSelect')+$t('processDialog.agent')+assigneeName,trigger:'blur'}]">
              <el-select v-model="currentNode.assignee"
                         multiple
                         @change="changeAssi">
                <el-option v-for="item in ao"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <!-- 审核节点处理人 -->
            <div v-if="nodeMaintain && currentNode.nodeType === 3">
              <el-form-item :label="$t('processDialog.handler')">
                <el-select multiple
                           v-model="currentNode.assignedTo">
                  <el-option v-for="item in maintainOption"
                             :key="item.value"
                             :label="item.label"
                             :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item :label="$t('processDialog.alternativeProcessor')">
                <el-select multiple
                           v-model="currentNode.candidate">
                  <el-option v-for="item in maintainOption"
                             :key="item.value"
                             :label="item.label"
                             :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </div>
            <!-- 节点权限 -->
            <el-form-item :label="$t('processDialog.nodePermission')"
                          v-show="!nodeMaintain">
              <el-checkbox v-model="currentNode.isEditable">{{$t('processDialog.edit')}}</el-checkbox>
              <el-checkbox :disabled="deleteAble"
                           v-model="currentNode.isDeletable">{{$t('processDialog.delete')}}</el-checkbox>
              <el-checkbox v-model="currentNode.isAddable">{{$t('processDialog.addNodeAfter')}}</el-checkbox>
            </el-form-item>
            <!-- 回退 -->
            <el-form-item :label="$t('processDialog.back')"
                          v-if="currentNode.nodeType === 3 || currentNode.nodeType === 4">
              <el-checkbox v-model="isBack"
                           @change="changeBack">{{$t('processDialog.canBack')}}</el-checkbox>
              <el-select v-show="isBack"
                         v-model="currentNode.backTo"
                         @change="changeBackNode">
                <el-option v-for="item in nodeOption"
                           :key="item.id"
                           :label="item.nodeName"
                           :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <!-- 会签节点 -->
            <el-form-item :label="$t('processDialog.addAssigneeNode')"
                          v-show="currentNode.nodeType === 4">
              <el-table :data="relationData"
                        style="width: 100%"
                        class="table"
                        border>
                <el-table-column width="40"
                                 label=""
                                 align="center">
                  <template slot-scope="scope">
                    <el-tooltip v-if="scope.$index !== relationData.length - 1"
                                placement="top"
                                :content="$t('processDialog.deleteText')">
                      <el-button icon="el-icon-close"
                                 type="text"
                                 :disabled="!currentNode.isEditable && nodeMaintain"
                                 @click="deleteTable(scope.$index)"></el-button>
                    </el-tooltip>
                    <el-tooltip v-else
                                placement="top"
                                :content="$t('processDialog.addText')">
                      <el-button icon="el-icon-plus"
                                 type="text"
                                 :disabled="!currentNode.isEditable && nodeMaintain"
                                 @click="addTable(scope.$index)"></el-button>
                    </el-tooltip>
                  </template>
                </el-table-column>
                <el-table-column :label="$t('processDialog.assigneeAgentType')"
                                 align="center"
                                 width="140">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.assigneeType"
                               :disabled="!currentNode.isEditable && nodeMaintain"
                               @change="changeTableAssignee(scope.row.assigneeType,scope.$index)">
                      <el-option v-for="item in assigneeOption"
                                 :key="item.value"
                                 :label="item.label"
                                 :value="item.value"></el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column :label="$t('processDialog.agentPerson')"
                                 align="center">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.assignee"
                               :multiple="true"
                               @change="changeTableAssi(scope.row.assignee,scope.row.assigneeType,scope.$index)">
                      <el-option v-for="item in scope.row.assigneeOption"
                                 :key="item.value"
                                 :label="item.label"
                                 :value="item.value"></el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column v-if="nodeMaintain"
                                 :label="$t('processDialog.approvePerson')"
                                 align="center">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.assignedTo"
                               :multiple="true">
                      <el-option v-for="item in scope.row.maintainOption"
                                 :key="item.value"
                                 :label="item.label"
                                 :value="item.value"></el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column v-if="nodeMaintain"
                                 :label="$t('processDialog.agentApprovePerson')"
                                 align="center">
                  <template slot-scope="scope">
                    <el-select v-model="scope.row.candidate"
                               :multiple="true">
                      <el-option v-for="item in scope.row.maintainOption"
                                 :key="item.value"
                                 :label="item.label"
                                 :value="item.value"></el-option>
                    </el-select>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
            <!-- 超时设置 -->
            <div v-show="currentNode.nodeType === 3 || currentNode.nodeType === 4">
              <div class="border"></div>
              <span class="color-span">{{$t('processDialog.timeoutSetting')}}</span>
              <el-form-item>
                <div v-if="nodeMaintain">
                  <el-radio v-model="expire"
                            :disabled="!currentNode.isEditable && nodeMaintain"
                            :label="0">{{$t('processDialog.customTimeout')}}</el-radio>
                  <el-form-item v-show="expire === 0">
                    <el-input-number class="day-input"
                                     :controls="false"
                                     v-model="currentNode.expireIn"></el-input-number>
                    <el-select class="day-select"
                               v-model="currentNode.expireUnit">
                      <el-option v-for="item in dayOption"
                                 :key="item.value"
                                 :label="item.label"
                                 :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                  <el-radio v-model="expire"
                            :disabled="!currentNode.isEditable && nodeMaintain"
                            :label="1">{{$t('processDialog.specifyTimeOut')}}</el-radio>
                  <el-form-item v-show="expire === 1">
                    <el-date-picker v-model="currentNode.expireTime"
                                    type="datetime"
                                    value-format="yyyy-MM-dd hh:mm:ss"
                                    :placeholder="$t('processDialog.selectTimeout')"></el-date-picker>
                  </el-form-item>
                </div>
                <div v-else>
                  <el-checkbox v-model="isExpire">{{$t('processDialog.customTimeout')}}</el-checkbox>
                  <el-form-item v-show="isExpire">
                    <el-input-number class="day-input"
                                     :controls="false"
                                     v-model="currentNode.expireIn"></el-input-number>
                    <el-select class="day-select"
                               v-model="currentNode.expireUnit">
                      <el-option v-for="item in dayOption"
                                 :key="item.value"
                                 :label="item.label"
                                 :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </div>
                <el-checkbox v-model="currentNode.isTransferable">{{$t('processDialog.overTimeGetOtherPerson')}}</el-checkbox>
              </el-form-item>
            </div>
            <!-- 并行通过规则 -->
            <div v-show="currentNode.nodeType === 4">
              <div class="border"></div>
              <span class="color-span">{{$t('processDialog.passageRule')}}</span>
              <el-form-item label="">
                <el-radio-group v-model="radioType"
                                :disabled="!currentNode.isEditable && nodeMaintain">
                  <el-radio :label="1">1.{{$t('processDialog.nodeNumberPass')}}</el-radio>
                  <el-radio :label="2">2.{{$t('processDialog.specifyNodePass')}}</el-radio>
                </el-radio-group>
              </el-form-item>
              <div class="radio-container"
                   v-if="radioType === 1">
                <div v-for="(item,index) in currentLineArr"
                     :key="index">
                  <el-form-item :label="item.remark">
                    {{$t('processDialog.when')}}<el-input v-model="item.approvalsCnt"
                              style="width:100px;margin:0 10px;"
                              @blur="checkApprovals(index)">
                      <p slot="suffix">{{$t('processDialog.number')}}</p>
                    </el-input>{{$t('processDialog.passWhenPass')}}
                  </el-form-item>
                </div>
              </div>
              <div class="radio-container"
                   v-else>
                <div v-for="(item,index) in currentLineArr"
                     :key="index">
                  <!-- <span>{{item.remark}}</span> -->
                  <el-table :data="item.acousticData"
                            style="width: 100%"
                            class="table"
                            border>
                    <el-table-column :label="$t('processDialog.agentType')"
                                     align="center"
                                     prop="name">
                    </el-table-column>
                    <el-table-column :label="$('processDialog.mustPass')"
                                     align="center">
                      <template slot-scope="scope">
                        <el-checkbox v-model="scope.row.isMandatory"></el-checkbox>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </div>
            </div>
          </el-form>
        </el-tab-pane>
        <el-tab-pane v-if="currentNode.nodeType === 3 || currentNode.nodeType === 4 || currentNode.nodeType === 6"
                     :label="(currentNode.nodeType === 3 || currentNode.nodeType === 4)?$t('processDialog.userDecision'):$t('processDialog.sysDecision')"
                     name="2">
          <div class="pane">
            <el-form class="node-form">
              <div v-for="(item,index) in decision"
                   :key="index">
                <div class="border"></div>
                <el-form-item :label="$t('processDialog.decisionName')">
                  <el-input v-model="item.remark"
                            :disabled="!currentNode.isEditable && currentNode.lineDisabled"></el-input>
                </el-form-item>
                <el-form-item v-show="currentNode.nodeType === 6 && approvalData.length === 1"
                              :label="$t('processDialog.addBackTask')">
                  <el-select v-model="item.backendTask"
                             multiple
                             value-key="methodURL">
                    <el-option v-for="obj in backOption"
                               :value-key="obj.methodDesc"
                               :key="obj.methodURL"
                               :label="obj.methodDesc"
                               :value="obj"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item v-show="currentNode.nodeType === 6 "
                              :label="$t('processDialog.decisionCondition')"
                              :disabled="!currentNode.isEditable && currentNode.lineDisabled">
                  <el-select v-model="item.taskCdtId"
                             :disabled="item.taskDisabled">
                    <el-option v-for="obj in item.taskOption"
                               :key="obj.id"
                               :label="obj.cdtName"
                               :value="obj.id"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item :label="$t('processDialog.afterExecutionPro')">
                  <el-select :disabled="!currentNode.isEditable && currentNode.lineDisabled"
                             v-model="item.wfStatusTo">
                    <el-option v-for="item in status"
                               :key="item.id"
                               :label="item.statusName"
                               :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item :label="$t('processDialog.afterExecutionAge')">
                  <el-table style="width: 100%"
                            class="table"
                            :data="item.objStatusTo"
                            border>
                    <el-table-column :label="$t('processDialog.objectType')"
                                     align="center">
                      <template slot-scope="scope">
                        <el-select v-model="scope.row.busiFuncId"
                                   readonly
                                   disabled
                                   class="disable-select">
                          <el-option-group v-for="item in busiOption"
                                           :key="item.id"
                                           :label="item.funcName">
                            <el-option v-for="obj in item.children"
                                       :key="obj.id"
                                       :label="obj.funcName"
                                       :value="obj.id"></el-option>
                          </el-option-group>
                        </el-select>
                      </template>
                    </el-table-column>
                    <el-table-column :label="$t('processDialog.childrenObjType')"
                                     align="center">
                      <template slot-scope="scope">
                        <el-select :disabled="!currentNode.isEditable && currentNode.lineDisabled"
                                   v-model="scope.row.funcTypeId"
                                   :multiple="true"></el-select>
                      </template>
                    </el-table-column>
                    <el-table-column :label="$t('processDialog.afterExecutionSta')"
                                     align="center">
                      <template slot-scope="scope">
                        <el-select :disabled="!currentNode.isEditable && currentNode.lineDisabled"
                                   v-model="scope.row.executionStatus">
                          <el-option v-for="item in scope.row.funcStatusOption"
                                     :key="item.id"
                                     :label="item.label"
                                     :value="item.value"></el-option>
                        </el-select>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-form-item>
              </div>
            </el-form>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-scrollbar>
    <div class="node-info-footer">
      <el-button type="primary"
                 @click="confirm">{{$t('processDialog.confirm')}}</el-button>
      <el-button @click="close">{{$t('processDialog.cancel')}}</el-button>
    </div>
  </div>
</template>

<script>
import { getUserByOrg, deptRoleList, getList, getDeptList, getCallBackUrl } from '../../api/processManagement/' // 获取用户
import { deepClone } from '../../util/util'
import { getUserInfo } from '../../api/processManagement/flowInstance' // 根据代理类型获取用户
import { getTaskConfigByBusi } from '../../api/processManagement/taskConfig'
import { getAuditStateList } from '../../api/processManagement/flowTemp'

export default {
  props: {
    currentNode: {
      type: Object,
      default: {}
    },
    nodeList: {
      type: Array
    },
    lineList: {
      type: Array
    },
    orgId: {
      type: Number
    },
    tempId: {
      type: Number
    },
    status: {
      type: Array,
      default: []
    },
    approvalData: {
      type: Array,
      default: []
    },
    busiOption: {
      type: Array,
      default: []
    },
    nodeMaintain: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    currentNode: {
      handler: 'updateValue',
      immediate: true,
      deep: true
    },
    relationData: {
      handler: 'getAcousticData',
      deep: true
    },
    radioType: {
      handler: 'changeRadioType'
    },
    expire () {
      if (this.expire === 0) this.currentNode.expireTime = ''
      else this.currentNode.expireIn = ''
    }
  },
  data () {
    return {
      type: '1',
      assigneeOption: [{
        label: this.$t('processDialog.user'),
        value: 1
      }, {
        label: this.$t('processDialog.role'),
        value: 2
      }, {
        label: this.$t('processDialog.position'),
        value: 3
      }, {
        label: this.$t('processDialog.dept'),
        value: 4
      }, {
        label: this.$t('processDialog.userGroup'),
        value: 5
      }],
      assigneeObj: {
        1: this.$t('processDialog.user'),
        2: this.$t('processDialog.role'),
        3: this.$t('processDialog.position'),
        4: this.$t('processDialog.dept'),
        5: this.$t('processDialog.userGroup')
      },
      ao: [],
      assigneeName: this.$t('processDialog.user'),
      nodeOption: [],
      isBack: false,
      isExpire: false,
      expire: 0,
      dayOption: [{
        label: this.$t('processDialog.day'),
        value: 'day'
      }, {
        label: this.$t('processDialog.hour'),
        value: 'hour'
      }],
      relationData: [],
      acousticData: [],
      radioType: 1,
      decision: [], // 决策数组,
      currentLineArr: [], // 当前节点连接流向数组
      maintainOption: [], // 任务审核人
      auditState: [], // 通用审核状态
      deleteAble: false, // 删除权限
      backOption: [], // 后台任务
    }
  },
  methods: {
    handleClick (tab) {
      this.type = tab.name
    },
    deleteTable (index) {
      this.relationData.splice(index, 1)
    },
    addTable (index) {
      this.relationData.push({ id: `${new Date().getTime()}`, assigneeOption: [], maintainOption: [] })
    },
    changeRadioType () {

    },
    // 检查Approvals是否是数字
    checkApprovals (index) {
      const o = ~~this.currentLineArr[index].approvalsCnt
      // if (`${o}` !== this.currentNode.approvalsCnt) this.$message.warning('分支通过规则格式填写错误，只能填写正整数')
      this.currentLineArr[index].approvalsCnt = o === 0 ? 1 : o
      if (o > this.currentLineArr[index].acousticData.length) {
        this.$message.warning(this.$t('processDialog.passNumberCannotOverNodeMax'))
        this.$set(this.currentLineArr[index], 'approvalsCnt', this.currentLineArr[index].acousticData.length)
      }
    },
    checkMandatory (nodeId, id) {
      if (nodeId !== '') {
        if (nodeId.indexOf(',') === -1) return nodeId === `${id}`
        let result = false
        Array.from(nodeId.split(',')).some(res => {
          if (res === `${id}`) {
            result = true
            return true
          }
        })
        return result
      }
      else return false
    },
    getAcousticData () {
      this.currentLineArr.some(res => {
        this.$set(res, 'acousticData', [])
        this.relationData.some(res2 => {
          const id = res2.id ? res2.id : `${new Date().getTime()}`
          res2.id = id
          res.acousticData.push({
            id,
            assignee: res2.assignee,
            assigneeType: res2.assigneeType,
            name: this.assigneeObj[res2.assigneeType],
            isMandatory: this.checkMandatory(res.ctsNodeId, res2.id),
            assignedTo: res2.assignedTo,
            candidate: res2.candidate
          })
        })
      })
    },
    changeAssignee (value) {
      this.getOption(value).then(res => {
        this.ao = res
      })
      this.currentNode.assignee = ''
      this.currentNode.assignedTo = ''
      this.currentNode.candidate = ''
      this.assigneeOption.some(res => {
        if (res.value === value) {
          this.assigneeName = res.label
          return true
        }
      })
    },
    changeTableAssignee (value, index) {
      let flag = false
      this.relationData.some((res, i) => {
        if (res.assigneeType === value && res.assigneeOption && index !== i) {
          flag = true
          return true
        }
      })
      if (flag) {
        this.$message.warning(this.$t('processDialog.assigneeNodeCannotRepeat'))
        return
      }
      this.getOption(value).then(res => {
        this.$set(this.relationData[index], 'assigneeOption', res)
        this.$set(this.relationData[index], 'assignee', '')
        this.$set(this.relationData[index], 'assignedTo', '')
        this.$set(this.relationData[index], 'candidate', '')
        this.$set(this.relationData[index], 'maintainOption', [])
      })
    },
    changeBack (back) {
      if (!back) {
        this.currentNode.backTo = ''
        this.decision.some(res => {
          if (`${res.fromId}` === `${this.currentNode.id}` && res.isBackLine === 1) {
            res.deleteLine = true
            return true
          }
        })
      }
    },
    // 切换回退节点、添加用户决策（回退决策）
    changeBackNode (id) {
      let flag = true
      this.decision.some(res => {
        if (`${res.fromId}` === `${this.currentNode.id}` && res.isBackLine === 1) {
          res.deleteLine = true
          res.continueLine = true
          flag = false
          this.$set(res, 'remark', this.$t('processDialog.back'))
          this.$set(res, 'toId', id)
          return true
        }
      })
      if (flag)
        this.decision.push({
          condition: '',
          deleteLine: false,
          fromId: this.currentNode.id,
          id: `${new Date().getTime()}`,
          objStatusTo: this.getObjStatus(),
          orgId: this.orgId,
          remark: this.$t('processDialog.back'),
          tempId: this.tempId,
          toId: id,
          wfStatusTo: '',
          isBackLine: 1,
          taskOption: []
        })
    },
    // 寻找该节点的可回退节点（仅可回退用户任务、会签任务）
    getBackNode (id) {
      if (!id) return
      this.lineList.some(res => {
        if (`${id}` === `${res.toId}` && res.isBackLine !== 1) {
          this.nodeOption = Array.from(new Set(this.nodeOption.concat(...this.nodeList.filter(item => { return `${item.id}` === `${res.fromId}` && (item.nodeType === 3 || item.nodeType === 4) }))))
          this.getBackNode(res.fromId)
        }
      })
    },
    // 根据新节点更新组件值
    updateValue (n, o) {
      // console.log(n)
      // 检验分支条件数改变权限控制
      if (this.lineList.filter(res => { return `${res.fromId}` === `${this.currentNode.id}` }).length > 1) {
        this.deleteAble = true
        this.currentNode.isDeletable = false
      } else {
        this.deleteAble = false
      }
      if (n.hasOwnProperty('id') && n.id !== o.id) {
        getAuditStateList().then(response => {
          this.auditState = []
          response.data.data.some(res => {
            this.auditState.push({
              label: res.description,
              value: res.code
            })
          })
          this.nodeOption = []
          this.getBackNode(this.currentNode.id)
          if (this.currentNode.nodeType === 4) {
            this.getParallelData()
            this.relationData = []
            if (this.currentNode.ctsNodes.length > 0)
              this.currentNode.ctsNodes.some((res, index) => {
                this.relationData.push({
                  ...res,
                  isMandatory: res.isMandatory === 1,
                  maintainOption: []
                })
                this.getOption(res.assigneeType).then(res => {
                  this.$set(this.relationData[index], 'assigneeOption', res)
                })
                if (this.nodeMaintain && Array.isArray(res.assignee))
                  this.getUserInfo(res.assignee.join(','), res.assigneeType, index).then(res => {
                    this.$set(this.relationData[index], 'maintainOption', res)
                  })
              })
            else this.relationData = [{ id: `${new Date().getTime()}`, assigneeOption: [], maintainOption: [] }]
          }
          else this.relationData = [{ id: `${new Date().getTime()}`, assigneeOption: [], maintainOption: [] }]
          if (this.currentNode.backTo !== '' && this.currentNode.backTo !== null) this.isBack = true
          else this.isBack = false
          if (this.nodeMaintain) {
            if (this.currentNode.expireIn !== 0 && this.currentNode.expireIn !== '') this.expire = 0
            else this.expire = 1
            this.maintainOption = []
            if (Array.isArray(this.currentNode.assignee))
              this.getUserInfo(this.currentNode.assignee.join(','), this.currentNode.assigneeType).then(res => {

                this.maintainOption = res
              })
          }
          else
            if (this.currentNode.expireIn !== 0 && this.currentNode.expireIn !== '') this.isExpire = true
            else this.isExpire = false
          this.getCallBackUrl()
          this.getDecisionData()
        })
      }
    },
    // 获取对象的审核回调接口
    getCallBackUrl () {
      if (this.approvalData.length !== 1 || this.currentNode.nodeType !== 6) {
        this.backOption = []
        return
      }
      getCallBackUrl(this.getBusiObj(this.busiOption, this.approvalData[0].busiFuncId).tableName).then(res => {
        this.backOption = res.data.data
      })
    },
    // 获取tableName
    getBusiObj (data, target, result = '') {
      if (Array.isArray(data))
        data.some(res => {
          if (res.children.length > 0) result = this.getBusiObj(res.children, target, result)
          if (res.id === target) {
            result = res
            return true
          }
        })
      return result
    },
    // 获取并行通过规则页面准备数据
    getParallelData () {
      this.currentLineArr = this.lineList.filter(res => { return `${res.fromId}` === `${this.currentNode.id}` && res.isBackLine !== 1 })
      if (this.currentNode.ctsConditions && this.currentNode.ctsConditions.length > 0) {
        this.currentLineArr.some((res, index) => {
          this.$set(res, 'approvalsCnt', this.currentNode.ctsConditions[index].approvalsCnt)
          this.$set(res, 'ctsNodeId', this.currentNode.ctsConditions[index].ctsNodeId)
        })
        this.getAcousticData()
        if (this.currentNode.ctsConditions[0].approvalsCnt !== 0 && this.currentNode.ctsConditions[0].approvalsCnt !== '') this.radioType = 1
        else this.radioType = 2
      }
    },
    // 获取代理数据集
    getOption (value) {
      return new Promise((resolve, reject) => {
        const result = []
        switch (value) {
          case 1: // 用户
            getUserByOrg(this.orgId).then(res => {
              if (Array.isArray(res.data.data)) {
                res.data.data.some(res => {
                  result.push({
                    label: res.identityName,
                    value: res.userId
                  })
                })
              }
              resolve(result)
            })
            break
          case 2: // 角色
            deptRoleList({ orgId: this.orgId }).then(res => {
              if (Array.isArray(res.data.data)) {
                res.data.data.some(res => {
                  result.push({
                    label: res.roleName,
                    value: res.roleId
                  })
                })
              }
              resolve(result)
            })
            break
          case 3: // 职位
            getList({ orgId: this.orgId }).then(res => {
              if (Array.isArray(res.data.data)) {
                res.data.data.some(res => {
                  result.push({
                    label: res.posName,
                    value: res.id
                  })
                })
              }
              resolve(result)
            })
            break
          case 4: // 部门
            getDeptList({ orgId: this.orgId }).then(res => {
              if (Array.isArray(res.data.data)) {
                res.data.data.some(res => {
                  result.push({
                    label: res.deptName,
                    value: res.id
                  })
                })
              }
              resolve(result)
            })
            break
          case 5: // 用户组
            break
          default:
            this.$message.error(this.$t('processDialog.errorProxyCode'))
            break
        }
      })
    },
    // 切换代理人/部门...
    changeAssi (value) {
      if (this.nodeMaintain) this.getUserInfo(value.join(','), this.currentNode.assigneeType).then(res => {

        this.maintainOption = res
      })
    },
    // 切换table代理人..
    changeTableAssi (value, type, index) {
      this.getUserInfo(value.join(','), type).then(res => {
        this.$set(this.relationData[index], 'maintainOption', res)
      })
    },
    // 根据代理类型获取用户
    getUserInfo (ids, type) {
      return new Promise((resolve, reject) => {
        const result = []
        if (!ids) {
          reject()
          return
        }
        getUserInfo({ ids, type, orgId: this.orgId }).then(res => {
          if (res.data.data)
            res.data.data.some(res => {
              result.push({
                label: res.identityName,
                value: res.userId
              })
            })
          resolve(result)
        })
      })
    },
    // 解析执行后状态
    getObjStatus (data) {
      let arr = data && data !== '[]' ? data : deepClone(this.approvalData)
      arr = typeof (arr) === 'string' ? JSON.parse(arr) : arr
      arr = JSON.stringify(arr.map(res => { return res.busiFuncId })) === JSON.stringify(this.approvalData.map(res => { return res.busiFuncId })) ? arr : deepClone(this.approvalData)
      if (Array.isArray(arr))
        arr.some(res => {
          this.$set(res, 'funcStatusOption', this.auditState)
        })
      return arr
    },
    // 获取决策数据
    getDecisionData () {
      this.decision = []
      this.lineList.some(res => {
        res.objStatusTo = JSON.parse(res.objStatusTo)
        try {
          res.backendTask = JSON.parse(res.backendTask)
        } catch (error) {
          res.backendTask = []
        }
        if (`${res.fromId}` === `${this.currentNode.id}`) this.decision.push({ ...res, objStatusTo: this.getObjStatus(res.objStatusTo), taskOption: res.taskOption || [] })
      })
      this.decision.some(res => {
        if (res.objStatusTo.length === 1) this.getTaskConfigByBusi(res)
        else res.taskDisabled = true
      })
    },
    // 获取决策条件选择项
    getTaskConfigByBusi (data) {
      getTaskConfigByBusi(data.objStatusTo[0].busiFuncId).then(res => {
        data.taskOption = res.data.data
      })
    },
    // 获取会签节点并行通过规则
    getCtsNodeId (res) {
      let result = ''
      if (this.radioType !== 1) {
        const arr = []
        res.acousticData.some(res => {
          if (res.isMandatory) {
            arr.push(res.id)
          }
        })
        result = arr.join(',')
      }
      return result
    },
    confirm () {
      if (this.currentNode.nodeType === 3 && Array.isArray(this.currentNode.assignee) && this.currentNode.assignee.filter(res => { return res !== null }).length === 0) {
        this.$message.warning(this.$t('processDialog.agent') + this.assigneeName + this.$t('processDialog.cannotEmpty'))
        return
      }
      if (this.currentNode.nodeType === 4) {
        let checkSet = '' // 并行规则校验
        let signCheck = false // 会签审批人校验
        this.currentLineArr.some((res, index) => {
          res.acousticData.some((res, index) => {
            if (this.nodeMaintain) {
              if (res.assignedTo.length === 0) signCheck = true
              this.currentNode.ctsNodes[index] = {
                ...this.currentNode.ctsNodes[index],
                assignedTo: Array.isArray(res.assignedTo) ? res.assignedTo.join(',') : res.assignedTo,
                candidate: Array.isArray(res.candidate) ? res.candidate.join(',') : res.candidate,
                nodeId: Number(this.currentNode.id),
                tempId: this.tempId,
                id: res.id,
                assignee: Array.isArray(res.assignee) ? res.assignee.join(',') : res.assignee,
                assigneeType: res.assigneeType,
                orgId: this.orgId
              }
            }
            else
              this.currentNode.ctsNodes[index] = {
                ...this.currentNode.ctsNodes[index],
                nodeId: Number(this.currentNode.id),
                tempId: this.tempId,
                id: res.id,
                assignee: Array.isArray(res.assignee) ? res.assignee.join(',') : res.assignee,
                assigneeType: res.assigneeType,
                orgId: this.orgId
              }
          })
          this.currentNode.ctsConditions[index] = {
            approvalsCnt: this.radioType === 1 ? res.approvalsCnt === 0 ? 1 : res.approvalsCnt : '',
            ctsNodeId: this.getCtsNodeId(res),
            nodeId: Number(this.currentNode.id),
            tempId: this.tempId,
            orgId: this.orgId,
            lineId: res.id
          }
          if (this.radioType === 1) checkSet = res.approvalsCnt
          else checkSet = this.getCtsNodeId(res)
        })
        if (signCheck) {
          this.$message.warning(this.$t('processDialog.assigneeNodeNotEmpty'))
          return
        }
        if (checkSet === 0 || checkSet === '') {
          this.$message.warning(this.$t('processDialog.passNotEmpty'))
          return
        }
        let reFlag = false
        this.relationData.some(res => {
          if (!res.assignee || !res.assigneeOption) {
            reFlag = true
            return true
          }
        })
        if (reFlag) {
          this.$message.warning(this.$t('processDialog.assigneeNodeInfoNotEmpty'))
          return
        }
      }
      if (this.currentNode.nodeType !== 4 && this.currentNode.nodeType !== 6 && this.nodeMaintain && this.currentNode.assignedTo.filter(res => { return res !== null }).length === 0) {
        this.$message.warning(this.$t('processDialog.handler') + this.$t('processDialog.cannotEmpty'))
        return
      }
      this.currentNode.candidate = this.currentNode.candidate ? this.currentNode.candidate.filter(res => { return res !== null }) : []
      if (this.currentNode.nodeType !== 4 && this.currentNode.nodeType !== 6 && this.nodeMaintain && new Set(this.currentNode.assignedTo.concat(this.currentNode.candidate)).size !== (this.currentNode.assignedTo.length + this.currentNode.candidate.length)) {
        this.$message.warning(this.$t('processDialog.handlerAndAPCannotSame'))
        return
      }
      if ((this.currentNode.nodeType === 3 || this.currentNode.nodeType === 4) && this.currentNode.isTransferable && this.nodeMaintain && this.currentNode.candidate.length === 0) {
        this.$message.warning(this.$t('processDialog.youHaveCheckTimeout'))
        return
      }
      if (Array.isArray(this.currentNode.assignee))
        this.currentNode.assignee = this.currentNode.assignee.join(',')
      this.currentNode.isAddable = ~~this.currentNode.isAddable
      this.currentNode.isEditable = ~~this.currentNode.isEditable
      this.currentNode.isDeletable = ~~this.currentNode.isDeletable
      if (this.currentNode.nodeType === 3 || this.currentNode.nodeType === 4) this.currentNode.isTransferable = ~~this.currentNode.isTransferable
      if (this.currentNode.nodeType === 6) {
        const checkTask = new Set(this.decision.filter(res => { return ~~res.taskCdtId !== 0 }).map(res => { return ~~res.taskCdtId }))
        if (checkTask.size !== this.decision.length && checkTask.size !== 0) {
          this.$message.warning(this.$t('processDialog.pleaseFill'))
          return
        }
      }
      this.$emit('confirmNode', this.decision)
    },
    close () {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.node-form {
  /deep/.el-form-item {
    height: auto !important;
  }
  /deep/.el-input__inner {
    height: 32px !important;
    line-height: 32px !important;
  }
}
.node-info {
  width: 500px;
  height: 100%;
  &-body {
    height: calc(100% - 51px);
    overflow: auto;
  }
  &-footer {
    border-top: 1px solid #e4e7ed;
    height: 50px;
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    justify-content: center;
  }
}
/deep/.el-tabs__nav {
  height: 50px;
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
}
/deep/.el-tab-pane {
  padding: 0 20px !important;
}
/deep/.el-tabs__nav-scroll {
  padding: 0 20px;
}
/deep/.el-tabs__nav {
  .is-disabled {
    color: #303133 !important;
    border-right: 1px solid #e4e7ed;
    margin: 10px 0;
  }
}
/deep/.el-tabs__item {
  height: 30px !important;
  line-height: 30px !important;
}
/deep/.el-select {
  width: 100%;
}
.border {
  border-top: 1px solid #e4e7ed;
  width: calc(100% + 40px);
  height: 1px;
  margin-left: -20px;
}
.color-span {
  font-size: 15px !important;
  display: inline-block;
  margin-top: 20px;
}
.day-input {
  margin-right: 10px;
  /deep/input {
    text-align: left;
  }
}
.day-select {
  width: 100px;
}
.table {
  /deep/th {
    height: 32px !important;
    line-height: 32px !important;
    border-color: #ebeef5;
    color: #303133;
    background: #fafafa;
  }
  /deep/.el-select {
    width: 100%;
    input {
      border: none !important;
      height: 36px !important;
      line-height: 36px !important;
    }
  }
}
/deep/.el-input__suffix {
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  justify-content: center;
}
.radio-container {
  margin: 10px 0;
}
.disable-select {
  /deep/div {
    border-right: 0;
    /deep/input {
      border-color: inherit !important;
      background-color: inherit !important;
      color: #606266 !important;
    }
  }
}
/deep/.el-checkbox {
  margin: 0 30px 0 0 !important;
}
/deep/.el-form-item__label {
  width: 100%;
  text-align: left;
  height: 36px !important;
  line-height: 36px !important;
  float: none;
}
/deep/.el-form-item__content,
/deep/.el-input__inner {
  height: auto !important;
  line-height: 36px !important;
}
</style>
